package com.ukao.pad.ui.splash;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.ukao.pad.R;
import com.ukao.pad.base.BaseFragment;
import com.ukao.pad.ui.setting.WifiListFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;
import info.hoang8f.android.segmented.SegmentedGroup;

/**
 * 引导页面链接无线网
 */
public class NetworkTabFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.btn_wireless)
    RadioButton btnWireless;
    @BindView(R.id.btn_wired)
    RadioButton btnWired;
    @BindView(R.id.wifi_segmented)
    SegmentedGroup wifiSegmented;
    @BindView(R.id.layout)
    FrameLayout layout;
    Unbinder unbinder;
    @BindView(R.id.next)
    TextView next;


    private String mParam1;
    private String mParam2;
    private int mCurrentPosition;
    private FragmentTransaction fragmentTransaction;


    public static NetworkTabFragment newInstance(String param1, String param2) {
        NetworkTabFragment fragment = new NetworkTabFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_link_wifi, container, false);
        unbinder = ButterKnife.bind(this, view);
        controlShowFragment(1);
        return view;
    }

    private void controlShowFragment(int position) {
        FragmentManager fragmentManager = getChildFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        Fragment oldFragment = fragmentManager.findFragmentByTag(makeTag(mCurrentPosition));
        if (oldFragment != null) {
            fragmentTransaction.hide(oldFragment);
        }
        mCurrentPosition = position;

        Fragment currentFragment = fragmentManager.findFragmentByTag(makeTag(position));
        if (currentFragment != null) {
            fragmentTransaction.show(currentFragment);
        } else {

            addFragment(position);
        }
        fragmentTransaction.commitAllowingStateLoss();


    }

    @OnCheckedChanged({R.id.btn_wireless, R.id.btn_wired})
    public void OnCheckedChanged(CompoundButton view, boolean check) {
        if (check)
            switch (view.getId()) {
                case R.id.btn_wireless:
                    controlShowFragment(1);
                    break;
                case R.id.btn_wired:
                    controlShowFragment(2);
                    break;


            }
    }


    private String makeTag(int position) {
        return R.id.layout + "" + position;
    }

    private void addFragment(int position) {
        switch (position) {

            case 1:
                fragmentTransaction.add(R.id.layout, WifiListFragment.newInstance("", ""), makeTag(position));

                break;
            case 2:
                fragmentTransaction.add(R.id.layout, WiredFragment.newInstance("", ""), makeTag(position));
                break;

        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.next:

                break;

        }
    }
}
